﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QualityStormCommon;
using QualityStormDesignerApplication;

namespace QualityStorm {
    public partial class FrmProcessModels : Form {

        ProcessModel m_ProcessModel = null;
        ConnectionContextBag m_contextBag;

        bool m_ReadOnly = false;

        public bool ReadOnly {
            get {
                return m_ReadOnly;
            }
            set {
                m_ReadOnly = value;
                SetReadOnly();
            }
        }

        private void SetReadOnly() {
            txtName.Enabled = !m_ReadOnly;
            grdPermissions.Enabled = !m_ReadOnly;
            cboViewMode.Enabled = !m_ReadOnly;
            btnOK.Enabled = !m_ReadOnly;
            
        }


        public ConnectionContextBag ContextBag {
            get {
                return m_contextBag;
            }
            set {
                m_contextBag = value;
            }
        }

        public ProcessModel ProcessModel {
            get {
                return m_ProcessModel;
            }
            set {
                m_ProcessModel = value;
                ShowData();
            }
        }

        private void ShowData() {
            if (m_ProcessModel != null) {
                txtName.Text = m_ProcessModel.Name;
                cboViewMode.SelectedIndex = (int)m_ProcessModel.DisplayMode;
                chkAutomatic.Checked =  m_ProcessModel.Automatic == true;
                chkManual.Checked =  m_ProcessModel.Manual == true;
                lstTriggers.Items.Clear();
                object[] triggers = m_contextBag.Context.Service.GetTriggers(m_ProcessModel.Guid);

                foreach (object trigger in triggers) {
                    lstTriggers.Items.Add(new TriggerWrapper(trigger));
                }

                
            }

        }

        public FrmProcessModels(ConnectionContextBag bag) {
            m_contextBag = bag;
            InitializeComponent();
        }


        public FrmProcessModels() {
            InitializeComponent();
            cboViewMode.SelectedIndex = 0;
        }

        private void btnEditCustomForm_Click(object sender, EventArgs e) {
            FrmDesigner frm = new FrmDesigner();
            frm.ShowDialog();

        }

        private void FrmProcessModels_Load(object sender, EventArgs e) {

        }

        private void btnOK_Click(object sender, EventArgs e) {
            if (txtName.Text == "") {
                MessageBox.Show("Please inform the name of the Process Model.", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            if (m_ProcessModel == null) {
                m_ProcessModel = ProcessModel.CreateProcessModel(Guid.NewGuid());
                m_ProcessModel.Name = txtName.Text;
                if (m_contextBag.Folder != null) {
                    m_ProcessModel.Parent = m_contextBag.Folder.Guid;
                } else {
                    m_ProcessModel.Parent = m_contextBag.Module.Guid;
                }
                m_ProcessModel.DisplayMode = (byte?) cboViewMode.SelectedIndex;
                m_ProcessModel.Automatic = chkAutomatic.Checked;
                m_ProcessModel.Manual = chkManual.Checked;

                m_contextBag.Context.Service.AddProcessModel(m_ProcessModel);
                m_contextBag.Context.Service.SaveChanges();

                // Saving Triggers
                for (int i = 0; i < lstTriggers.Items.Count; i++) {
                    if (((TriggerWrapper)lstTriggers.Items[i]).Trigger is ScheduleTrigger) {
                        ScheduleTrigger schTrigger = ((TriggerWrapper)lstTriggers.Items[i]).Trigger as ScheduleTrigger;
                        schTrigger.ProcessModelGuid = m_ProcessModel.Guid;
                        schTrigger.Order = i;
                        m_contextBag.Context.Service.AddScheduleTrigger(schTrigger);
                        m_contextBag.Context.Service.SaveChanges();

                    } else {
                        ValueBasedTrigger valTrigger = ((TriggerWrapper)lstTriggers.Items[i]).Trigger as ValueBasedTrigger;
                        valTrigger.ProcessModelGuid = m_ProcessModel.Guid;
                        valTrigger.Order = i;
                        m_contextBag.Context.Service.AddValueBasedTrigger(valTrigger);
                        m_contextBag.Context.Service.SaveChanges();


                    }
                }

            } else {
                m_ProcessModel.Name = txtName.Text;
                m_ProcessModel.Automatic = chkAutomatic.Checked;
                m_ProcessModel.Manual = chkManual.Checked;

                m_contextBag.Context.Service.ClearProcessModelTriggers(m_ProcessModel);
                m_contextBag.Context.Service.SaveChanges();


                  // Saving Triggers
                for (int i = 0; i < lstTriggers.Items.Count; i++) {
                    if (((TriggerWrapper)lstTriggers.Items[i]).Trigger is ScheduleTrigger) {
                        ScheduleTrigger schTrigger = ((TriggerWrapper)lstTriggers.Items[i]).Trigger as ScheduleTrigger;
                        schTrigger.ProcessModelGuid = m_ProcessModel.Guid;
                        schTrigger.Order = i;

                        m_contextBag.Context.Service.AddScheduleTrigger(schTrigger);

                        m_contextBag.Context.Service.SaveChanges();

                    } else {
                        ValueBasedTrigger valTrigger = ((TriggerWrapper)lstTriggers.Items[i]).Trigger as ValueBasedTrigger;
                        valTrigger.ProcessModelGuid = m_ProcessModel.Guid;
                        valTrigger.Order = i;

                        m_contextBag.Context.Service.SaveChanges();
                        m_contextBag.Context.Service.AddValueBasedTrigger(valTrigger);
                        m_contextBag.Context.Service.SaveChanges();

                    }
                }

                m_contextBag.Context.Service.UpdateProcessModel(m_ProcessModel);
                m_contextBag.Context.Service.SaveChanges();

            }

            DialogResult = DialogResult.OK;
            Close();

        }

      
        private void button1_Click(object sender, EventArgs e) {
            FrmOPCTagValueTriggerSetup frm = new FrmOPCTagValueTriggerSetup(m_contextBag);
            frm.ShowDialog();
        }

        private void btnAddTrigger_Click(object sender, EventArgs e) {

        }

        private void btnAddTrigger_MouseDown(object sender, MouseEventArgs e) {
            Point p = new Point(btnAddTrigger.Width,0);
            ctxAddTriggerButton.Show(btnAddTrigger, p);
        }

        private void oPCTagValueBasedTriggerToolStripMenuItem_Click(object sender, EventArgs e) {
            FrmOPCTagValueTriggerSetup frm = new FrmOPCTagValueTriggerSetup(this.ContextBag);
            frm.ContextBag = this.ContextBag;
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK) {
                lstTriggers.Items.Add(new TriggerWrapper(frm.Trigger));
            }
        }

        private void scheduleTriggerToolStripMenuItem_Click(object sender, EventArgs e) {
            FrmScheduleTrigger frm = new FrmScheduleTrigger();
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK) {
                lstTriggers.Items.Add(new TriggerWrapper(frm.Trigger));
            }
        }

        private void chkAutomatic_CheckedChanged(object sender, EventArgs e) {
            lstTriggers.Enabled = chkAutomatic.Checked;
            btnAddTrigger.Enabled = chkAutomatic.Checked;
           

        }

        private void lstTriggers_SelectedIndexChanged(object sender, EventArgs e) {
            btnEditTrigger.Enabled = chkAutomatic.Checked;
            btnDeleteTrigger.Enabled = chkAutomatic.Checked;

        }

        private void btnEditTrigger_Click(object sender, EventArgs e) {
            TriggerWrapper wrapper = lstTriggers.SelectedItem as TriggerWrapper;
            if (wrapper.Trigger is ScheduleTrigger) {
                FrmScheduleTrigger frm = new FrmScheduleTrigger();
                frm.Trigger = wrapper.Trigger as ScheduleTrigger;
                frm.ShowDialog();
                if (frm.DialogResult == DialogResult.OK) {
                    lstTriggers.Items[lstTriggers.SelectedIndex] = new TriggerWrapper(frm.Trigger);
                }
            } else {
                FrmOPCTagValueTriggerSetup frm = new FrmOPCTagValueTriggerSetup();
                frm.ContextBag = ContextBag;
                frm.Trigger = wrapper.Trigger as ValueBasedTrigger;
                frm.ShowDialog();
                if (frm.DialogResult == DialogResult.OK) {
                    lstTriggers.Items[lstTriggers.SelectedIndex] = new TriggerWrapper(frm.Trigger);
                }
            }
            lstTriggers.Refresh();
            
        }

        private void btnDeleteTrigger_Click(object sender, EventArgs e) {
            lstTriggers.Items.Remove(lstTriggers.SelectedItem);
        }

        private void addSectionToolStripMenuItem_Click(object sender, EventArgs e) {
            FrmSection frm = new FrmSection();
            frm.ShowDialog();
        }
       
    }

    class TriggerWrapper {
        object m_Trigger;

        public object Trigger {
            get {
                return m_Trigger;
            }
        }
        public TriggerWrapper(object Trigger) {
            m_Trigger = Trigger;
        }

        public override string ToString() {
            string toString = "";
            if (m_Trigger is ScheduleTrigger) {
                ScheduleTrigger schTrigger = m_Trigger as ScheduleTrigger;
                toString = "Schedule Trigger";

                ScheduleOptions option = (ScheduleOptions)schTrigger.Type;

                switch (option) {
                    case ScheduleOptions.Everyday:
                        toString += ", Everyday";
                        break;

                    case ScheduleOptions.LastDayOfTheMonth:
                        toString += ", Every Last Day of the Month";
                        break;

                    case ScheduleOptions.Sunday:
                        toString += ", Every Sunday";
                        break;

                    case ScheduleOptions.Monday:
                        toString += ", Every Monday";
                        break;

                    case ScheduleOptions.Tuesday:
                        toString += ", Every Tuesday";
                        break;

                    case ScheduleOptions.Wednesday:
                        toString += ", Every Wednesday";
                        break;

                    case ScheduleOptions.Thursday:
                        toString += ", Every Thursday";
                        break;

                    case ScheduleOptions.Friday:
                        toString += ", Every Friday";
                        break;

                    case ScheduleOptions.Saturday:
                        toString += ", Every Saturday";
                        break;

                    case ScheduleOptions.SpecificDayOfTheMonth:
                        toString += ", Every " + schTrigger.Day.ToString();
                        string day = schTrigger.Day.ToString();
                        char lastChar = day[day.Length - 1];
                        switch (lastChar) {
                            case '1':
                                toString += "st";
                                break;

                                case '2':
                                toString += "nd";
                                break;

                                case '3':
                                toString += "rd";
                                break;

                            default:
                                toString += "th";
                                break;  

                                
                        }
                        break;


                }

                toString += ", From " + schTrigger.InitialHour.Value.ToString("hh:mm:ss.ffff");
                toString += ", To " + schTrigger.FinalHour.Value.ToString("hh:mm:ss.ffff");
                toString += ", Periodicity " + schTrigger.Periodicity.Value.ToString("hh:mm:ss.ffff");

                return toString;
                
            } else if (m_Trigger is ValueBasedTrigger) {
                ValueBasedTrigger valTrigger = m_Trigger as ValueBasedTrigger;
                toString += "OPC Tag Trigger, Based on " + valTrigger.OPCTagName;
                return toString;
            
            }
            return "Error";
        }
    }
}
